package com.fht.service.promotion.impl;

import com.fht.entity.PromotionMealHot;
import com.fht.mapper.promotion.PromotionMealHotMapper;
import com.fht.service.promotion.PromotionMealHotService;
import com.fht.util.UnitConversionUtil;
import com.fht.vo.LayUITableVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class PromotionMealHotServiceImpl implements PromotionMealHotService {


    @Resource
    public PromotionMealHotMapper promotionMealHotMapper;

    @Override
    public LayUITableVO<List<PromotionMealHot>> findPage(Map<String, Object> map) {

        PageHelper.startPage((int) map.get("page"),(int) map.get("limit"));



        List<PromotionMealHot> list = promotionMealHotMapper.findPage(map);
        for (PromotionMealHot promotionMealHot : list) {
            convert(promotionMealHot);
        }

        PageInfo<PromotionMealHot> pageInfo = new PageInfo<>(list);

        LayUITableVO<List<PromotionMealHot>> vo = new LayUITableVO<>();
        vo.setCount(pageInfo.getTotal());
        vo.setData(pageInfo.getList());


        return vo;
    }

    @Override
    public PromotionMealHot selectById(String id) {

        PromotionMealHot promotionMealHot = promotionMealHotMapper.selectByPrimaryKey(id);
        convert(promotionMealHot);

        return promotionMealHot;
    }

    @Override
    @Transactional
    public void update(PromotionMealHot promotionMealHot) {

        convertContrary(promotionMealHot);
        PromotionMealHot old = promotionMealHotMapper.selectByPrimaryKey(promotionMealHot.getId());
        if (old == null){
            promotionMealHotMapper.insertSelective(promotionMealHot);
        }else {
            promotionMealHotMapper.updateByPrimaryKeySelective(promotionMealHot);
        }



    }

    @Override
    @Transactional
    public void deleteById(String id) {
        promotionMealHotMapper.deleteByPrimaryKey(id);
    }


    public void convert(PromotionMealHot promotionMealHot){
        promotionMealHot.setPriceString(UnitConversionUtil.conversion(promotionMealHot.getPrice()));
        if (promotionMealHot.getStatus()==0){
            promotionMealHot.setStatusString("下架");
        }else if (promotionMealHot.getStatus() == 1){
            promotionMealHot.setStatusString("上架");
        }
        if (promotionMealHot.getPrintLabel()==0){
            promotionMealHot.setPrintLabelString("关闭");
        }else if (promotionMealHot.getPrintLabel() == 1){
            promotionMealHot.setPrintLabelString("开启");
        }

    }
    public void convertContrary(PromotionMealHot promotionMealHot){
        promotionMealHot.setPrice(UnitConversionUtil.conversion(promotionMealHot.getPriceString()));
        if ("".equals(promotionMealHot.getStatus()) || null == promotionMealHot.getStatus()){
            promotionMealHot.setStatus(0);
        }
        if ("".equals(promotionMealHot.getPrintLabel()) || null == promotionMealHot.getPrintLabel()){
            promotionMealHot.setPrintLabel(0);
        }
        if ("".equals(promotionMealHot.getManualDiscount()) || null == promotionMealHot.getManualDiscount()){
            promotionMealHot.setManualDiscount(0);
        }
    }
}
